from flask import Blueprint, request, render_template
import requests
import json

from methods.xhsdOs import import_by_xls, publishProductSingle_all
# 浙江新华书店
from utils.tools import dcc_book_detail, parse_number

bp = Blueprint("xhsdOs", __name__, url_prefix="/xhsdOs")


@bp.route('/test', methods=['get'])
def test():
    return "test"


@bp.route('/publishProduct', methods=['post'])
def publishProduct():
    data = request.get_json()
    dcc_token = data.get('dcc_token')
    serial_no = data.get('serial_no')
    session_id = data.get('session_id')
    discount = data.get('discount')
    if not dcc_token or not serial_no or not session_id:
        return {'code': 0, 'message': '请传入必填项！'}
    book_details_json = dcc_book_detail(serial_no, dcc_token)
    if book_details_json['code'] != 200:
        return {'code': 0, 'message': book_details_json['message']}
    book_details_json = book_details_json['data']
    return import_by_xls(book_details_json, session_id, discount)


@bp.route('/publishProductSingle', methods=['post'])
def publishProductSingle():
    data = request.get_json()
    dcc_token = data.get('dcc_token')
    session_id = data.get('session_id')
    discount = data.get('discount')
    self_id = data.get('self_id')  # 商家编码
    serial_no = data.get('serial_no')
    if not dcc_token or not session_id or not self_id or not discount or not serial_no:
        return {'code': 0, 'message': '请传入必填项！'}
    book_details_json = dcc_book_detail(serial_no, dcc_token)
    if book_details_json['code'] != 200:
        return {'code': 0, 'message': book_details_json['message']}
    book_details_json = book_details_json['data']
    return publishProductSingle_all(book_details_json, session_id, discount, self_id)

# @bp.route('/publishProductSingle', methods=['post'])
# def publishProductSingle():
#     data = request.get_json()
#     dcc_token = data.get('dcc_token')
#     serial_no = data.get('serial_no')
#     session_id = data.get('session_id')
#     discount = data.get('discount')
#     if not dcc_token or not serial_no or not session_id:
#         return {'code': 0, 'message': '请传入必填项！'}
#     book_details_json = dcc_book_detail(serial_no, dcc_token)
#     if book_details_json['code'] != 200:
#         return {'code': 0, 'message': book_details_json['message']}
#     book_details_json = book_details_json['data']
#     return import_by_single_base(book_details_json, session_id, discount)
